'''
Description: 
version: 1.0.0
Author: peng2.zhang
Date: 2020-11-23 17:22:32
LastEditors: peng2.zhang
LastEditTime: 2021-05-31 16:55:31
'''
# coding: utf-8

import ujson


class BaseException(Exception):

    def __init__(self, code, message, data):
        super().__init__(f"{code}({message})", data)
        self.code = code
        self.message = message
        self.data = data

    def get_return_object(self):
        return {
                'code': self.code,
                'message': self.message,
                'data': self.data
            }

    def __unicode__(self):
        return ujson.dumps(self.get_return_object())

    def __str__(self):
        return self.__unicode__()
    
    def __repr__(self):
        return self.__unicode__()


class FailureException(BaseException):

    def __init__(self, err, data):
        code = err.errcode
        message = err.errvalue
        super().__init__(code, message, data)
        self.code = code
        self.message = message
        self.data = data
